<?php

namespace tmcore\services;

use think\facade\Request;
use tmcore\utils\Helper;

class HttpService
{
    /**
     * 错误信息
     * @var string
     */
    private static $curlError;

    /**
     * header头信息
     * @var string
     */
    private static $curlHeader;

    /**
     * 返回信息
     * @var array
     */
    private static $curlContent;

    /**
     * 请求信息
     * @var array
     */
    private static $curlInfo;

    /**
     * 获取请求错误信息
     * @return string
     */
    public static function getRequestError()
    {
        return self::$curlError;
    }

    /**
     * 获取请求响应信息
     * @return mixed
     */
    public static function getInfo()
    {
        return self::$curlInfo;
    }

    /**
     * 获取返回信息
     * @return mixed
     */
    public static function getCurlContent()
    {
        return self::$curlContent;
    }

    /**
     * 获取header头字符串类型
     * @return mixed
     */
    public static function getHeaderStr(): string
    {
        return self::$curlHeader;
    }

    /**
     * 获取header头数组类型
     * @return array
     */
    public static function getHeader(): array
    {
        return explode("\r\n", self::$curlHeader);
    }

    /**
     * GET 发起请求
     * @param string $url 请求地址
     * @param array $data 请求数据
     * @param array $header header头
     * @param int $timeout 响应超时时间
     * @param bool $isJson 是否json请求
     * @param bool $isDownload 是否下载请求
     * @return bool|string
     */
    public static function getRequest($url, $data = [], $header = [], $timeout = 10, $isJson = false, $isDownload = false)
    {
        if (!empty($data)) {
            $url .= (stripos($url, '?') === false ? '?' : '&');
            $url .= (is_array($data) ? http_build_query($data) : $data);
        }

        return self::request($url, 'get', [], $header, $timeout, $isJson, $isDownload);
    }

    /**
     * POST 发起请求
     * @param string $url 请求链接
     * @param array $data 请求参数
     * @param array $header header头
     * @param int $timeout 超时秒数
     * @param bool $isJson 是否json请求
     * @param bool $isDownload 是否下载请求
     * @return bool|string
     */
    public static function postRequest($url, array $data = [], $header = [], $timeout = 10, $isJson = false, $isDownload = false)
    {
        return self::request($url, 'post', $data, $header, $timeout, $isJson, $isDownload);
    }

    /**
     * curl 请求
     * @param string $url 请求地址
     * @param string $method 请求方式
     * @param array $data 请求数据
     * @param array $header 请求header头
     * @param int $timeout 超时秒数
     * @param bool $isJson 是否json请求
     * @param bool $isDownload 是否下载请求
     * @return bool|string
     */
    public static function request($url, $method = 'get', $data = [], $header = [], $timeout = 10, $isJson = false, $isDownload = false)
    {
        self::$curlInfo = null;
        self::$curlError = null;
        self::$curlHeader = null;

        $curl = curl_init($url);
        $method = strtoupper($method);
        //请求方式
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

        $headerArr = $header ? $header : [];
        //json请求
        if ($isJson) {
            $data = json_encode($data);
            array_push($headerArr, 'Content-Type: application/json', 'Content-Length: ' . strlen($data));
            if ($header) $headerArr = array_unique($headerArr);
        }

        //post请求
        if ($method == 'POST') {
            if (is_array($data)) $data = http_build_query($data);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }

        //超时时间
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        //设置header头
        if ($header) curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);

        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        //返回抓取数据
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //设置referer
        curl_setopt($curl, CURLOPT_REFERER, Request::root(true));
        if(!$isDownload) {
            //输出header头信息
            curl_setopt($curl, CURLOPT_HEADER, true);
            //TRUE 时追踪句柄的请求字符串，就是允许你查看请求header
            curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        }
        //https请求
        if (1 == strpos("$" . $url, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        self::$curlError = curl_error($curl);
        list($content, $curlInfo) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
        self::$curlInfo = $curlInfo;
        self::$curlHeader = trim(substr($content, 0, $curlInfo['header_size']));
        if(!$isDownload) $content = trim(substr($content, $curlInfo['header_size']));
        self::$curlContent = $content;
        return (intval($curlInfo["http_code"]) === 200) ? $content : false;
    }
}